package com.rmxx.fruit.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.company.android.fruit.R;
import com.kwan.xframe.common.widget.observablescrollview.ObservableRecyclerView;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;
import com.rmxx.fruit.bean.Product;

import java.util.ArrayList;
import java.util.List;

public class OrderConfirmActivity extends BaseCommonActivity {

	ObservableRecyclerView mRecyclerView;
	List<Product> mProduct = new ArrayList<>();
	TextView tv_pay;
	private BaseQuickAdapter<Product, BaseViewHolder> mBaseAdapter;
	@Override
	protected BasePresenter getBasePresenter() {
		return null;
	}

	@Override
	protected void initViews() {
		super.initViews();

		Product product = new Product();
		product.setRicon(R.mipmap.product4_01);
		product.setName("【2袋】张君雅拉面丸子100g");
		product.setDesc("我的火辣不要轻易挑战哦");
		product.setPrice(6.66f);
		mProduct.add(product);


		product = new Product();
		product.setRicon(R.mipmap.product4_02);
		product.setName("真丽斯沐浴露持久留香");
		product.setDesc("洗澡就能护肤");
		product.setPrice(48.00f);
		mProduct.add(product);

		product = new Product();
		product.setRicon(R.mipmap.product4_03);
		product.setName("瑞松秋刀鱼即食海鲜类鱼干");
		product.setDesc("嚼劲十足 鲜香炭烤 深海捕捞");
		product.setPrice(48.00f);
		mProduct.add(product);

		mRecyclerView = findViewById(com.kwan.xframe.R.id.recycleView);
		mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
		mRecyclerView.setItemAnimator(new DefaultItemAnimator());
		mRecyclerView.setHasFixedSize(true);

		//int spanCount = 2;
		//int spacing = 20;
		//mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, true));
		//麻痹的 关闭硬件加速 否则 cardView 报错

		mRecyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		mRecyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		mBaseAdapter = new BaseQuickAdapter<Product, BaseViewHolder>(R.layout.list_item_order_confirm,mProduct) {
			@Override
			protected void convert(BaseViewHolder helper, final Product item) {
				helper.setOnCheckedChangeListener(R.id.cb_all, new CompoundButton.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						item.setChecked(isChecked);

						mRecyclerView.post(new Runnable() {
							@Override
							public void run() {
								mBaseAdapter.notifyDataSetChanged();
							}
						});


					}
				});

				helper.setOnCheckedChangeListener(R.id.cb_item, new CompoundButton.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						item.setChecked(isChecked);
						mRecyclerView.post(new Runnable() {
							@Override
							public void run() {
								mBaseAdapter.notifyDataSetChanged();
							}
						});
					}
				});


				helper.setChecked(R.id.cb_all,item.isChecked());
				helper.setChecked(R.id.cb_item,item.isChecked());
			}
		};

		mBaseAdapter.addFooterView(getLayoutInflater().inflate(R.layout.layout_order_amount, null));
		mRecyclerView.setAdapter(mBaseAdapter);

		findViewById(R.id.v_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		((CheckBox)findViewById(R.id.cb_all)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				for(Product p:mProduct)
					p.setChecked(isChecked);
				mBaseAdapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	protected void initData() {

	}

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_order_confirm;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected String getTitleTxt() {
		return null;
	}

	@Override
	public String getPageName() {
		return null;
	}
}
